# -*- coding: utf-8 -*-
"""
Module containing the InfoGetter class

This is an attempt to define a common interface for all possible online
market information sources. 
"""

import numpy
import datetime
from misc.misc import enum
import misc.quoteperiod as qp

class SymbolsItem(object):
    """
    Stores one row of the result of the InfoGetter.GetSymbols() operation.
    
    This class has fields that will probably be specific to the TradeZone
    client.
    """
    # String; Originally the field called "Bolsa".
    # One example of this is "BOVESPA"
    supermarket = None
    # String. Example: "VISTA"
    market = None
    # String. Example: "PETR4"
    symbol = None
    # String. Example: "Petrobras"
    description = None
    # Boolean; unknown use
    flagDivide1000 = False
    # String; unknown use
    group = None

    def GetName(self):
        return "%s_%s_%s" % (self.supermarket, self.market, self.symbol)


######################class Status:
######################    """Object returned by InfoGetter communication methods."""
######################    flagOk = True
######################    # Error message, in case of error (i.e., flagOk is False).
######################    msg = ""
######################    # Result, in case the communication method is supposed to return something, 
######################    # and there was no error
######################    result = None
######################    
######################    def __init__(self, error=None, result=None):
######################        """Initializes with optional error*/result setting (mutually exclusive*).
######################        
######################        * Error has priority
######################        
######################        Arguments:
######################          error -- string, just the error message
######################          result -- some object, depending on the operation that is returning the
######################                    Status object
######################        
######################        """
######################        if error is not None:
######################            self.SetError(error)
######################        elif result is not None:
######################            self.SetResult(result)
######################    
######################    def SetResult(self, result):
######################        self.result = result
######################        self.flagOk = True
######################    
######################    def SetError(self, msg, E=None):
######################        """Sets flagOk flag to false and self.msg to a string using msg and E.
######################        
######################        Arguments:
######################           msg -- string
######################           E -- Exception object
######################        
######################        """
######################        
######################        if E is None:
######################            self.msg = msg
######################        else:
######################            self.msg = "%s: [[%s]]" % (msg, E.__str__())
######################
######################        self.flagOk = False
######################        print msg # This is for debugging purposes

class InfoGetter(object):
    """Common class for all information getters.
    
    Rules for all interface methods (methods that are supposed to be called
    through Pyro proxy):
      1) They all  must return a Status object, implying that
      2) They must catch any exceptions, and
      3) in this case, pass information about the exception in the Status object
         (Status.SetError() is for that).
        
    """
    
    def Ping(self):
        return "pong"
        
    def GetSymbols(self, pattern=""):
        """Returns list of symbols matching given pattern.
        
        Each symbol is returned as a SymbolsItem object - Abstract.
        
        pattern -- string. Empty pattern should return all items
        
        """
        raise NotImplementedError("Not implemented")
        
    def GetHistory(self, symbol, from_, to, \
                   period=qp.min1):
        """Gets historical data as timedata.Quotes object - Abstract method."""
        raise NotImplementedError("Not implemented")
